package com.yabushan.web.controller.system;

import java.util.List;

import com.yabushan.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yabushan.common.annotation.Log;
import com.yabushan.common.core.controller.BaseController;
import com.yabushan.common.core.domain.AjaxResult;
import com.yabushan.common.enums.BusinessType;
import com.yabushan.system.domain.YmxOrderInfo;
import com.yabushan.system.service.IYmxOrderInfoService;
import com.yabushan.common.utils.poi.ExcelUtil;
import com.yabushan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单Controller
 *
 * @author yabushan
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/ymx/ymxorderinfo")
public class YmxOrderInfoController extends BaseController {
    @Autowired
    private IYmxOrderInfoService ymxOrderInfoService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(YmxOrderInfo ymxOrderInfo) {
        startPage();
        List<YmxOrderInfo> list = ymxOrderInfoService.selectYmxOrderInfoList(ymxOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YmxOrderInfo ymxOrderInfo) {
        List<YmxOrderInfo> list = ymxOrderInfoService.selectYmxOrderInfoList(ymxOrderInfo);
        ExcelUtil<YmxOrderInfo> util = new ExcelUtil<YmxOrderInfo>(YmxOrderInfo.class);
        return util.exportExcel(list, "ymxorderinfo");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(ymxOrderInfoService.selectYmxOrderInfoById(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/order")
    public AjaxResult add(@RequestBody YmxOrderInfo ymxOrderInfo) {
        ymxOrderInfo.setOrderId(StringUtils.getUUID());
        int result=0;
        //判断订单是否存在，如果存在则不允许新增
        YmxOrderInfo requestOrderInfo = new YmxOrderInfo();
        requestOrderInfo.setAmazonOrderId(ymxOrderInfo.getAmazonOrderId());
        List<YmxOrderInfo> ymxOrderInfos = ymxOrderInfoService.selectYmxOrderInfoList(requestOrderInfo);
        if(ymxOrderInfos.size()>0){
            return  AjaxResult.error("已存在订单号");
        }else{
             result = ymxOrderInfoService.insertYmxOrderInfo(ymxOrderInfo);
        }

        return toAjax(result);
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YmxOrderInfo ymxOrderInfo) {
        return toAjax(ymxOrderInfoService.updateYmxOrderInfo(ymxOrderInfo));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds) {
        return toAjax(ymxOrderInfoService.deleteYmxOrderInfoByIds(orderIds));
    }

    @Log(title = "批量导入订单信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ymx:ymxorderinfo:importDataOrder')")
    @PostMapping("/importDataOrder")
    @ApiOperation(value = "批量导入订单信息")
    public AjaxResult importDataOrder(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<com.yabushan.web.model.entity.YmxOrderInfo> util = new ExcelUtil<com.yabushan.web.model.entity.YmxOrderInfo>(com.yabushan.web.model.entity.YmxOrderInfo.class);
        List<com.yabushan.web.model.entity.YmxOrderInfo> userList = util.importExcel(file.getInputStream());
        int size = userList.size();
        if (size != 0) {
            for (int i = 0; i < size; i++) {
                com.yabushan.web.model.entity.YmxOrderInfo ymxOrderInfo = userList.get(i);
                if (!StringUtils.isEmpty(ymxOrderInfo.getAmazonOrderId())) {
                    YmxOrderInfo orderInfo = new YmxOrderInfo();
                    orderInfo.setOrderId(StringUtils.getUUID());
                    orderInfo.setAmazonOrderId(ymxOrderInfo.getAmazonOrderId());
                    orderInfo.setMerchantOrderId(ymxOrderInfo.getMerchantOrderId());
                    orderInfo.setPurchaseDate(ymxOrderInfo.getPurchaseDate());
                    orderInfo.setOrderStatus(ymxOrderInfo.getOrderStatus());
                    orderInfo.setSalesChannel(ymxOrderInfo.getSalesChannel());
                    orderInfo.setOrderChannel(ymxOrderInfo.getOrderChannel());
                    orderInfo.setUrl(ymxOrderInfo.getUrl());
                    orderInfo.setShipServiceLevel(ymxOrderInfo.getShipServiceLevel());
                    orderInfo.setProductName(ymxOrderInfo.getProductName());
                    orderInfo.setSku(ymxOrderInfo.getSku());
                    orderInfo.setAsin(ymxOrderInfo.getAsin());
                    orderInfo.setItemStatus(ymxOrderInfo.getItemStatus());
                    orderInfo.setQuantity(ymxOrderInfoService.selectLont(ymxOrderInfo.getQuantity()));
                    if (ymxOrderInfo.getCurrency() != null) {
                        orderInfo.setCurrency(ymxOrderInfo.getCurrency().toString());
                    }
                    if (!StringUtils.isEmpty(ymxOrderInfo.getItemPrice())) {
                        orderInfo.setItemPrice(Double.parseDouble(ymxOrderInfo.getItemPrice()));
                    }

                    orderInfo.setItemTax(ymxOrderInfoService.selectLont(ymxOrderInfo.getItemTax()));
                    orderInfo.setShippingPrice(ymxOrderInfoService.selectLont(ymxOrderInfo.getShippingPrice()));
                    orderInfo.setShippingTax(ymxOrderInfoService.selectLont(ymxOrderInfo.getShippingTax()));
                    orderInfo.setGiftWrapPrice(ymxOrderInfoService.selectLont(ymxOrderInfo.getGiftWrapPrice()));
                    orderInfo.setGiftWrapTax(ymxOrderInfoService.selectLont(ymxOrderInfo.getGiftWrapTax()));
                    orderInfo.setItemPromotionDiscount(ymxOrderInfo.getItemPromotionDiscount());
                    orderInfo.setShipPromotionDiscount(ymxOrderInfo.getShipPromotionDiscount());
                    orderInfo.setShipCity(ymxOrderInfo.getShipCity());
                    orderInfo.setShipPostalCode(ymxOrderInfo.getShipPostalCode());
                    orderInfo.setShipState(ymxOrderInfo.getShipState());
                    orderInfo.setShipCountry(ymxOrderInfo.getShipCountry());
                    orderInfo.setPromotionIds(ymxOrderInfo.getPromotionIds());
                    orderInfo.setIsBusinessOrder(ymxOrderInfo.getIsBusinessOrder());
                    orderInfo.setPurchaseOrderNumber(ymxOrderInfo.getPurchaseOrderNumber());
                    orderInfo.setPriceDesignation(ymxOrderInfo.getPriceDesignation());


                    //判断订单是否存在，如果存在则更新，如果不存在则新增
                    YmxOrderInfo requestOrderInfo = new YmxOrderInfo();
                    requestOrderInfo.setAmazonOrderId(ymxOrderInfo.getAmazonOrderId());
                    List<YmxOrderInfo> ymxOrderInfos = ymxOrderInfoService.selectYmxOrderInfoList(requestOrderInfo);
                    if(ymxOrderInfos.size()>0){
                        //修改
                        YmxOrderInfo ymxOrderInfo1 = ymxOrderInfos.get(0);
                        orderInfo.setOrderId(ymxOrderInfo1.getOrderId());
                        ymxOrderInfoService.updateYmxOrderInfo(orderInfo);
                    }else{
                        //新增
                        ymxOrderInfoService.insertYmxOrderInfo(orderInfo);
                    }


                }
            }
        }

        // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //String operName = loginUser.getUsername();
        //String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success("批量导入客户信息");
    }
}
